package com.coocaa.ops.admin.v1.homepage.provider.impl;

import com.coocaa.ops.admin.v1.dmp.biz.impl.DmpRegistryRlsInfoBizImpl;
import com.coocaa.ops.admin.v1.homepage.biz.impl.HomepageModuleRlsBizImpl;
import com.coocaa.ops.admin.v1.homepage.convert.HomeSaveConverter;
import com.coocaa.ops.admin.v1.homepage.model.internal.HomeBatchSaveModel;
import com.coocaa.ops.admin.v1.homepage.provider.IHomepageInfoRlsUpdateProvider;
import com.coocaa.ops.common.core.base.enums.ResourceTypeEnum;
import com.coocaa.ops.common.core.base.mybatis.QueryTemplate;
import com.coocaa.ops.common.core.biz.dmp.entity.DmpRegistryRlsInfoEntity;
import com.coocaa.ops.common.core.biz.homepage.entity.HomeModuleContentConfigInfo;
import com.coocaa.ops.common.core.biz.homepage.entity.HomepageInfoEntity;
import com.coocaa.ops.common.core.biz.homepage.entity.HomepageModuleRlsEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author bjh
 * @description 首页信息原始表 关联信息更新提供者接口实现
 * @date : 2020-06-15 16:31:15
 */
@Service
public class HomepageInfoRlsUpdateProviderImpl implements IHomepageInfoRlsUpdateProvider{
    @Autowired
    private HomepageModuleRlsBizImpl homepageModuleRlsBiz;
    @Autowired
    private DmpRegistryRlsInfoBizImpl dmpRegistryRlsInfoBiz;

    @Override
    public void saveRls(HomepageInfoEntity inputEntity, Integer userId) {
        HomeBatchSaveModel saveModel = new HomeBatchSaveModel();
        List<HomeModuleContentConfigInfo> moduleList = inputEntity.getModuleList();
        if(CollectionUtils.isEmpty(moduleList)){
            return;
        }
        for (HomeModuleContentConfigInfo homeModuleContentConfigInfo : moduleList){
             if (Objects.equals(1, homeModuleContentConfigInfo.getModuleIsDmp())){
                 //如果是dmp内容
                 handleDmpContent(inputEntity, homeModuleContentConfigInfo, saveModel);
             }else {
                 HomepageModuleRlsEntity homepageModuleRlsEntity = HomeSaveConverter.getHomepageModuleRlsEntity(inputEntity, homeModuleContentConfigInfo);
                 homepageModuleRlsEntity.setSeq(saveModel.getModuleSeq());
                 saveModel.getFinalHomeModuleList().add(homepageModuleRlsEntity);
             }
            saveModel.setModuleSeq(saveModel.getModuleSeq() + 1);
        }
        dmpRegistryRlsInfoBiz.saveBatchAvoidEmpty(saveModel.getFinalDmpList());
        homepageModuleRlsBiz.saveBatchAvoidEmpty(saveModel.getFinalHomeModuleList());
    }

    @Override
    public void deleteAllRls(Integer id, Integer version) {
        QueryTemplate<HomepageModuleRlsEntity> queryTemplate = QueryTemplate.getEqQueryTemplate("flag", 1);
        queryTemplate.lambda().eq(HomepageModuleRlsEntity::getHomepageId, id)
                .eq(HomepageModuleRlsEntity::getHomepageVersion, version);
        homepageModuleRlsBiz.getMapper().delete(queryTemplate);
        dmpRegistryRlsInfoBiz.deleteDmpRls(id, ResourceTypeEnum.HOMEPAGE.getValue(), version);
    }

    /*************** ===私有方法=== ********************* ===start=== ***************************************/
    private void handleDmpContent(HomepageInfoEntity inputEntity, HomeModuleContentConfigInfo moduleInfo,   HomeBatchSaveModel saveModel){
        if (moduleInfo.getDmpRegistryInfo() == null) {
            saveModel.getFinalHomeModuleList().add(HomeSaveConverter.getHomepageModuleRlsEntity(inputEntity, moduleInfo));
            return;
        }
        DmpRegistryRlsInfoEntity dmpInfo = HomeSaveConverter.getDmpRegistryRlsInfoEntity(inputEntity, moduleInfo);
        dmpInfo.setSubContainerSeq(saveModel.getModuleSeq());
        dmpInfo.setSubContainerContentSeq(1);
        saveModel.getFinalDmpList().add(dmpInfo);


    }


}
